/**
 * Created by zhouli on 18/2/7
 * Email li.zhou@huilianyi.com
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import {deepFullCopy} from "share/common";
//待添加的字段类型
let customFromOriginList = [];
//待添加的字段类型，常用的字段类型
let customFromOriginListWithOften = [];
//常用扩展字段类型名称
let customFromOriginListWithOftenName = [];
//值列表,
/*
* 需求 6928
* 后端给一个写死的值列表类型 systemCustomEnumerationType
* 前端获取之后，通过 systemCustomEnumerationType 去在值列表中找 customEnumerationOID
* 找到之后，放到写死的值列表里面
* */
let allEnumerationsList = [];
export default {
  //获取扩展字段表单
  getPersonCustomForm() {
    let params = {
      formCode: "user_attach_form",
      roleType: "TENANT"
    }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/custom/forms/by/form/code', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //统一返回,需要的数据
  getViewDataForExtendFiled() {
    return new Promise((resolve, reject) => {
      let res = {
        allEnumerationsList,
        customFromOriginList,
        customFromOriginListWithOften,
        customFromOriginListWithOftenName
      };
      resolve(res)
    })
  },
  getWidgetListAll() {
    //获取
    // 一般扩展字段类型
    // 常用扩展字段
    // 常用扩展字段类型名称
    return Promise.all([
      this.getCustomEnumerationInTenant(),
      this.getWidgetList({type: 1003}),
      this.getWidgetList({type: 1004}),
      this.getParameterTypes()
    ])
  },
  /**
   * 得到所有值列表
   * 这里返回的结果是租户的系统值列表 + 租户的自定义值列表
   * @param page
   * @param size
   */
  getCustomEnumerationInTenant() {
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/custom/enumerations/by/custom/form?page=0&size=100`)
        .then((res) => {
          allEnumerationsList = res.data;
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //获取表单类型(此函数不动，其他模块在用)
  getWidgetList(params) {
    // let params = {
    //   type:1003,一般扩展字段类型
    //   type:1004,常用扩展字段
    // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/form/gui/widgets/all', params)
        .then((res) => {
          if (params.type + "" === "1003") {
            customFromOriginList = [];
            let data = deepFullCopy(res.data);
            data.map((item) => {
              customFromOriginList.push(item)
            });
          } else if (params.type + "" === "1004") {
            customFromOriginListWithOften = [];
            let data = deepFullCopy(res.data);
            // 通过这个区分是常用扩展字段类型，
            // fieldCode = "heliosspc_regulation"
            data.map((item) => {
              customFromOriginListWithOften.push(item)
            })
          }
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取常用扩展字段类型名称
  getParameterTypes() {
    return new Promise((resolve, reject) => {
      httpFetch.get(`${config.baseUrl}/api/custom/enumerations/template/by/type?type=1013`)
        .then((res) => {
          customFromOriginListWithOftenName = res.data;
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //获取扩展字段表单
  getCustomForm(oid) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/custom/forms/' + oid + '/simple')
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //创建个人信息扩展字段表单
  createCustomForm: function (form) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/users/v2/custom/forms', form)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //保存表单
  updateCustomForm(form) {
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/custom/forms', form)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  }

}
